<?php
/**
 * User Model
 * @author libai.tc@gmail.com
 */

class auserModel
{
    private $_db;
    
    public function __construct() {
        $this->_db = new DB();
    }

	public function verify($username, $password) {
		$password_md5=md5($password);
		if($this->_db->get_one("select * from auser where password='$password_md5'"))
			return true;
		else
			return false;
	}
    
    public function getAuser() {
        $result=$this->_db->get_all('select `id`,`username`,`email` from auser ');
        if(empty($result)) return array('total'=>0, 'rows'=>array());
        return json_encode($result);
    }
    public function saveAuser() {
        $username=Base::getRequest('username');
        $email=Base::getRequest('email');
        $password=md5(Base::getRequest('password'));
    
        $sql="insert into `auser` (`username`, `password`, `email`) values ('$username', '$password', '$email')";
        
        if($this->_db->query($sql)) return array('success'=>false);
        return json_encode(array(
            'id' => mysql_insert_id(),
            'username' => $username,
            'email' => $email,
        ));
    }
    public function updateAuser() {
        $id=Base::getRequest('id');
        $username=Base::getRequest('username');
        $email=Base::getRequest('email');
        $password=md5(Base::getRequest('password'));
    
        $sql="update `auser` set `username`='$username', `email`='$email', `password`='$password' where `id`=$id";

        if($this->_db->query($sql)) return array('success'=>false);
        return json_encode(array(
            'id' => $id,
            'username' => $username,
            'email' => $email,
        ));
    }
    public function destroyAuser() {
        $p_id=Base::getRequest('id');
        $sql="delete from `auser` where `id`=$p_id";
		if(!$this->_db->query($sql)){
			return json_encode(array('success'=>false));
		}
		echo json_encode(array('success'=>true));
	}
}

/* ------ EOF ------ */
